#ifndef _PACKET_LOST_H_
#define _PACKET_LOST_H_

#include "../define/define.h"
#include "../util/lock.h"

class packet_lost
{
public:
	packet_lost()
	{
		func_lock locker;
		reset();
	}

	void add_rpy_count(uint32 rpy_count,uint32 tick)
	{
		func_lock locker;
		m_rpy_count = m_rpy_count + rpy_count;
		calc(tick);
	}

	void add_req_count(uint32 req_count,uint32 tick)
	{
		func_lock locker;
		m_req_count = m_req_count + req_count;
		calc(tick);
	}

	void calc(uint32 tick)
	{
		func_lock locker;
		if(tick- m_last_tick < 200)
		   return;

		m_rate = 100.0f * (m_rate + m_rpy_count) / (100 + m_req_count);

		if(m_rate > 100.0f)
		m_rate = 100.0f;

		m_rpy_count = 0;
		m_req_count = 0;
		m_last_tick = tick;
	}

	void reset()
	{
		func_lock locker;
		m_req_count = 0;
		m_rpy_count = 0;
		m_last_tick = 0;
		m_rate = 100.0f;
	}

	float get_rate()
	{
		func_lock locker;
		return m_rate;
	}

private:
	uint32 m_req_count;
	uint32 m_rpy_count;

	uint32 m_last_tick;
	float  m_rate;
};

#endif